package com.graduate.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.Future;
import java.util.logging.Logger;
/**
 * 异步调用测试接口
 * @Createtime : 2019-12-26
 * @Creator : jingchu
 */
@Service
public class AsyncServiceImp implements AsyncService {
    private static Random random = new Random();
    private Logger logger;

    @Async//单独开启子线程
    @Override
    public Future<String> Task1() throws Exception {
        System.out.println("Task1开始执行");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        logger.info("Task1耗时:" + String.valueOf(end - start) + "毫秒");
        return new AsyncResult<>("Task1结束执行");
    }

    @Async//单独开启子线程
    @Override
    public Future<String> Task2() throws Exception {
        System.out.println("Task2开始执行");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        logger.info("Task2耗时:" + String.valueOf(end - start) + "毫秒");
        return new AsyncResult<>("Task2结束执行");
    }

    @Async//单独开启子线程
    @Override
    public Future<String> Task3() throws Exception {
        System.out.println("Task3开始执行");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        logger.info("Task3耗时:" + String.valueOf(end - start) + "毫秒");
        return new AsyncResult<>("Task3结束执行");
    }
}
